package com.chengfei.spring.cloud.weather.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.Reader;
import java.io.StringReader;

/**
 * @author chengfei cheng.fei@chinaott.onaliyun.com
 **/
public class XmlBuilder {
    /**
     * 将xml转为指定的POJO
     * @param clazz
     * @param xmlStr
     * @return
     * @throws Exception
     */
   public static Object xmlStrToObject(Class<?> clazz,String xmlStr) throws Exception{
       Object xmlObject = null;
       JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
       //XML转为对象的接口
       Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
       Reader reader  = new StringReader(xmlStr);
       xmlObject = unmarshaller.unmarshal(reader);
       if(null != reader){
           reader.close();
       }
       return xmlObject;
   }
}
